/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/binary-tree-maximum-path-sum
   @Language: C++
   @Datetime: 19-08-29 14:32
   */

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
	int dfs(TreeNode *root, int &mx){
		if(root==NULL) return 0;
		int left=max(dfs(root->left,mx),0);
		int right=max(dfs(root->right,mx),0);
		mx=max(root->val+left+right,mx);
		return root->val+max(left,right);
	}
	public:
	int maxPathSum(TreeNode* root) {
		int mx=INT_MIN;
		dfs(root, mx);
		return mx;
	}
};
